# -*- coding: utf-8 -*-
"""
Created on Mon May 14 19:45:31 2018

@author: Alex
"""
import os.path
import os
import time
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf
from midas import Midas

cont_path = 'kropko/data_tmp/'
# bin_path = 'kropko/bin/'
# ord_path = 'kropko/ord/'
# nom_path = 'kropko/nom/'

results_path1 = 'kropko/lowcorr_results.csv'
results_path2 = 'kropko/highcorr_results.csv'

if os.path.exists(results_path1):
  os.remove(results_path1)

if os.path.exists(results_path2):
  os.remove(results_path2)


def waiting(path, name):
#  print("Now awaiting", name)
  dots = "."
  while not os.path.exists(path+'marker.csv'):
    if os.path.exists(results_path1) & os.path.exists(results_path2):
      break
    if dots == "......":
      dots = "      "
    elif dots == "      ":
      dots = "."
    else:
      dots += "."
    time.sleep(0.5)
#    print("\rWaiting"+dots, end="")
#  print("\rInit...       ")


def reverse_dummies(imputation, cat_cols):
  target_cols = imputation[cat_cols]
  imputation.drop(cat_cols, axis=1, inplace= True)
  imputation['y_1'] = target_cols.idxmax(axis=1)
  return imputation


def write_imputations(path, imputer, shape,
                      tx_mean, tx_std, nominal_cols= None):
  imputations = imputer.generate_samples(m=10).output_list
  n = 1
  for i in imputations:
    file_out = path + "imp" + str(n) + ".csv"
    if nominal_cols is not None:
      i = reverse_dummies(i, nominal_cols)
    i = i.mul(tx_std).add(tx_mean)
    assert i.shape[0] == shape
    i.fillna(0).to_csv(file_out, index=False)
    n +=1

  time.sleep(2)
  os.remove(path+'marker.csv')

data_shape = 1000

while not os.path.exists(results_path1) & os.path.exists(results_path2):
  waiting(cont_path, "continuous")
  if os.path.exists(results_path1) & os.path.exists(results_path2):
    break
  data = pd.read_csv(cont_path+"data.csv")
  d_mean = data.mean()
  d_std = data.std()*2
  data = data.sub(d_mean).div(d_std)
  assert data.shape[0] == data_shape
  imp = Midas([1024], input_drop= .5, learn_rate=1e-4, vae_layer=False,
              weight_decay= 1e-5, train_batch= 8)
  imp.build_model(data).train_model(training_epochs= 5, verbose=True)
#  imp = Midas([1024], input_drop= 0.5, learn_rate=1e-4, vae_layer=False,
#              weight_decay= 1e-5,
#              train_batch= 16, dropout_level= 0.5)
#  imp.build_model(data).train_model(training_epochs= 1, verbose=False)

  write_imputations(cont_path, imp, data_shape,
                    tx_mean = d_mean, tx_std= d_std)
